package com.myblog.demo.controller;

import com.myblog.demo.common.AjaxResult;
import com.myblog.demo.common.UserSessionTools;
import com.myblog.demo.entity.MessageSessionUser;
import com.myblog.demo.entity.UserInfo;
import com.myblog.demo.entity.vo.Friend;
import com.myblog.demo.entity.vo.MessageSession;
import com.myblog.demo.service.FollowService;
import com.myblog.demo.service.MessageService;
import com.myblog.demo.service.MessageSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/message_session")
public class MessageSessionController {
    @Autowired
    private MessageSessionService messageSessionService;

    @Autowired
    private MessageService messageService;

    @Autowired
    private FollowService followService;


    /**
     * 获取当前登录用户的会话列表
     *
     * @param request HttpServletRequest 获取当前登录用户 id
     * @return AjaxResult -》List<MessageSession>
     */
    @RequestMapping("get_session_list")
    public AjaxResult getSessionList(HttpServletRequest request) {
        // 获取当前登录用户id
        UserInfo loginUser = UserSessionTools.getLoginUser(request);
        if (loginUser == null) {
            return AjaxResult.fail(-1, "当前用户未登录！");
        }

        int userId = loginUser.getId();

        // 要返回的会话列表
        List<MessageSession> messageSessions = new ArrayList<>();

        // 获取包含当前用户的所有 sessionId
        List<Integer> sessionIds = messageSessionService.getSessionIdByUId(userId);
        for (Integer sessionId : sessionIds) {
            // 创建 MessageSession 对象
            MessageSession messageSession = new MessageSession();
            messageSession.setSessionId(sessionId);
            // 获取当前对话中的另一个用户
            Friend friend = messageSessionService.getFriendBySessionId(sessionId, userId);
            messageSession.setFriend(friend);
            // 将这个会话添加到 messageSessions 中
            messageSessions.add(messageSession);
        }

        // 通过 AjaxResult 返回 messageSessions
        return AjaxResult.success(messageSessions);
    }

    /**
     * 新增会话
     *
     * @param friendId 私信的对象id
     * @param request  HttpServletRequest
     * @return AjaxResult -》 sessionId
     */
    @RequestMapping("/add_session")
    @Transactional
    public AjaxResult addSession(Integer friendId, HttpServletRequest request) {
        // 参数校验
        if (friendId == null || friendId <= 0) {
            return AjaxResult.fail(-1, "参数错误！");
        }

        // 获取当前登录用户id
        UserInfo loginUser = UserSessionTools.getLoginUser(request);
        if (loginUser == null) {
            return AjaxResult.fail(-1, "当前用户未登录！");
        }

        int userId = loginUser.getId();

        // 判断二者是否互相关注，只有互相关注的用户才能进行聊天
        Boolean flag = followService.isFollowEachOther(userId, friendId);
        if (!flag) {
            // 两个用户没有互相关注，返回 -1，不能进行私信
            return AjaxResult.fail(-1, "只有互相关注才能进行私信！");
        }

        // 判断这两个用户之间是否已经存在会话了
        flag = messageSessionService.isExistsSessionId(friendId, userId);
        if (flag) {
            return AjaxResult.fail(-1, "两个用户之间已经存在会话！");
        }

        // 新增会话
        MessageSession messageSession = new MessageSession();

        if (messageSessionService.addMessageSession(messageSession) != 1) {
            return AjaxResult.fail(-1, "创建会话失败！");
        }

        // 获取 sessionId
        Integer sessionId = messageSession.getSessionId();

        if (sessionId <= 0) {
            return AjaxResult.fail(-1, "创建 sessionId 失败！");
        }

        // 在 message_session_user 表中新增两条记录

        // userId
        MessageSessionUser messageSessionUser1 = new MessageSessionUser();
        messageSessionUser1.setSessionId(sessionId);
        messageSessionUser1.setUserId(userId);
        messageSessionService.addMessageSessionUser(messageSessionUser1);

        // friendId
        MessageSessionUser messageSessionUser2 = new MessageSessionUser();
        messageSessionUser2.setSessionId(sessionId);
        messageSessionUser2.setUserId(friendId);
        messageSessionService.addMessageSessionUser(messageSessionUser2);

        // 返回 sessionId
        return AjaxResult.success(sessionId);
    }

}
